package com.moji.util;

import com.moji.entity.SysMenu;
import com.moji.entity.dto.TreeMenuDto;
import com.moji.enums.MoJiEnumConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class MenuTreeUtil {
    /**
     * list集合的行数据，转换成 tree 结构
     * @param list CcpSystemMenu
     * @return
     */
    public static List<TreeMenuDto> list2Tree(List<SysMenu> list) {
        List<TreeMenuDto> result = new ArrayList<>();
        if(!CollectionUtils.isEmpty(list)){
            for(SysMenu m : list){
                if(m.getParentCode() == null || "0".equals(m.getParentCode())) {
                    TreeMenuDto sub = new TreeMenuDto();
                    sub.setId(m.getId().toString());
                    sub.setMenuName(m.getMenuName());
                    sub.setMenuCode(m.getMenuCode());
                    sub.setParentCode(m.getParentCode());
                    sub.setParentName(m.getParentName());
                    sub.setSort(m.getSort());
                    sub.setLevels(m.getLevels().toString());
                    sub.setMenuFlag(m.getMenuFlag());
                    sub.setUrl(m.getUrl());
                    sub.setPrOnoff(m.getPrOnoff());
                    sub.setIcon(m.getIcon());
                    sub.setSfOutside(m.getSfOutside());
                    sub.setShowStatus(m.getShowStatus());
                    sub.setSfCache(m.getSfCache());
                    sub.setMenuRoute(m.getMenuRoute());
                    sub.setRouteName(m.getRouteName());
                    sub.setRouteParameter(m.getRouteParameter());
                    sub.setComponentPath(m.getComponentPath());
                    sub.setPermissionCharacter(m.getPermissionCharacter());
                    sub.setTreeList(recurrence(list, m));
                    result.add(sub);
                }else{
                    //TreeDao vo = new TreeDao(m.getId().toString(),m.getMenuCode(), m.getMenuName(),m.getParentCode(),m.getParentName(),m.getSort().toString(),m.getLevels().toString(),m.getMenuFlag(),m.getUrl());
                    //result.add(vo);
                }
            }
        }
        return result;
    }
    /**
     *  递归函数
     * @param list
     * @param vo
     */
    private static List<TreeMenuDto> recurrence(List<SysMenu> list, SysMenu vo) {
        List<TreeMenuDto> subNodes = new ArrayList<>();
        for (SysMenu m : list) {
            if(MoJiEnumConfig.TGW_SYSTEM_PLATFORM_CODE.getCode().equals(vo.getSystemCode())){
                if (vo.getId().toString().equals(m.getParentCode())) {
                    TreeMenuDto sub = new TreeMenuDto();
                    sub.setId(m.getId().toString());
                    sub.setMenuName(m.getMenuName());
                    sub.setMenuCode(m.getMenuCode());
                    sub.setParentCode(m.getParentCode());
                    sub.setParentName(m.getParentName());
                    sub.setSort(m.getSort());
                    sub.setLevels(m.getLevels().toString());
                    sub.setMenuFlag(m.getMenuFlag());
                    sub.setUrl(m.getUrl());
                    sub.setPrOnoff(m.getPrOnoff());
                    sub.setIcon(m.getIcon());
                    sub.setSfOutside(m.getSfOutside());
                    sub.setShowStatus(m.getShowStatus());
                    sub.setSfCache(m.getSfCache());
                    sub.setMenuRoute(m.getMenuRoute());
                    sub.setRouteName(m.getRouteName());
                    sub.setRouteParameter(m.getRouteParameter());
                    sub.setComponentPath(m.getComponentPath());
                    sub.setPermissionCharacter(m.getPermissionCharacter());
                    sub.setTreeList(recurrence(list, m));
                    subNodes.add(sub);
                }
            }else{
                if (vo.getId().equals(m.getParentCode()) && vo.getSystemCode().equals(m.getSystemCode()) ) {
                    TreeMenuDto sub = new TreeMenuDto();
                    sub.setId(m.getId().toString());
                    sub.setMenuName(m.getMenuName());
                    sub.setMenuCode(m.getMenuCode());
                    sub.setParentCode(m.getParentCode());
                    sub.setParentName(m.getParentName());
                    sub.setSort(m.getSort());
                    sub.setLevels(m.getLevels().toString());
                    sub.setMenuFlag(m.getMenuFlag());
                    sub.setUrl(m.getUrl());
                    sub.setPrOnoff(m.getPrOnoff());
                    sub.setIcon(m.getIcon());
                    sub.setSfOutside(m.getSfOutside());
                    sub.setShowStatus(m.getShowStatus());
                    sub.setSfCache(m.getSfCache());
                    sub.setMenuRoute(m.getMenuRoute());
                    sub.setRouteName(m.getRouteName());
                    sub.setRouteParameter(m.getRouteParameter());
                    sub.setComponentPath(m.getComponentPath());
                    sub.setPermissionCharacter(m.getPermissionCharacter());
                    sub.setTreeList(recurrence(list, m));
                    subNodes.add(sub);
                }
            }
        }
        return subNodes;

    }

    /**
     * list集合的行数据，转换成 tree 结构
     * @param list CcpSystemMenu
     * @return
     */
    public static List<TreeMenuDto> list2TreeList(List<SysMenu> list) {
        List<TreeMenuDto> result = new ArrayList<>();
        if(!CollectionUtils.isEmpty(list)){
            for(SysMenu m : list){
                TreeMenuDto sub = new TreeMenuDto();
                sub.setId(m.getId().toString());
                sub.setMenuName(m.getMenuName());
                sub.setMenuCode(m.getMenuCode());
                sub.setParentCode(m.getParentCode());
                sub.setParentName(m.getParentName());
                sub.setSort(m.getSort());
                sub.setLevels(m.getLevels().toString());
                sub.setMenuFlag(m.getMenuFlag());
                sub.setUrl(m.getUrl());
                sub.setPrOnoff(m.getPrOnoff());
                sub.setIcon(m.getIcon());
                sub.setSfOutside(m.getSfOutside());
                sub.setShowStatus(m.getShowStatus());
                sub.setSfCache(m.getSfCache());
                sub.setMenuRoute(m.getMenuRoute());
                sub.setRouteName(m.getRouteName());
                sub.setRouteParameter(m.getRouteParameter());
                sub.setComponentPath(m.getComponentPath());
                sub.setPermissionCharacter(m.getPermissionCharacter());
                result.add(sub);
            }
        }
        return result;
    }
}
